#! /usr/bin/python

# birdHtml.py

"""Create an html format page of my birdlist database"""

# To do:
#     Make the pages only run a limited number of entries per page, say 10
#     Make the html page look nicer.
#     Perhaps do a little CSS and Js for a little magic :~)

import sqlite3 as lite
from bird import EachBird
import webbrowser
import os

class MakeHtml(EachBird):
    """Create an html file out of the birdlist
    
    That is stored on the database."""
    def __init__(self, dbase="", table=""):
        EachBird.__init__(self, dbase, table)
        
        # Connect to the database
        con = lite.connect(self.dbase)
        cur = con.cursor()
        
        # Basically we want all the values but the key
        query = "select cn, sn, day, month, year, place, town, country, notes from lifeList order by key desc";
        cur.execute(query)
        fatString = cur.fetchall()
        cur.close()
        con.close()
        
        # Because there are a number of them, make it easy to call.
        (cn, sn, day, month, year, place, town, country, notes) = range(9)
        
        # Litrally 'string' together the html page
        htmlBits = ["<!DOCTYPE html>",\
                      "<html>",\
                      "<head>",\
                      "<title>My Life List</title>",\
                      "<link rel='icon' type='image/gif' href='http://www.sandcurves.com/images/sandcurve.ico' />"
                      "</head><body>",\
                      "<h1>My Life List</h1>",
                      "<br/>"]
        for chunks in fatString:
            
            # Header from 
            htmlBits.append("<h2>%s <i>%s</i></h2>" % (chunks[cn], chunks[sn]))
            # main from dates and places
            htmlBits.append("<p>This lifer was seen on %s %s %s at %s in %s</p>" % (chunks[day], chunks[month], chunks[year], chunks[place], chunks[country]))
            # notes
            htmlBits.append("<p>Notes: &nbsp;%s</p>" % chunks[8])
        
        htmlBits.append("</body></html>")
    
        htmlOut = "\n".join(htmlBits)
        # I need to make it so that the user can specify the filename
        webs_path = os.getcwd()
        web_file = "%s%s%s" % (webs_path, os.sep, 'life_list.htm')
        webout = open(web_file, 'w')
        webout.write(htmlOut)
        webout.close()
        
    def showInBrowser(self):
        webbrowser.open('life_list.htm')

if __name__ == "__main__":
    MakeHtml(Provide_test_DB, Provide_test_Table)